var strGetQuery =document.location.search;
var roleid = cw.common.getQueryString(strGetQuery,'id');
$(document).ready(function(){
	if(roleid == 1){
		alert('不允许修改超级管理员角色信息');
		window.history.back();
	}
	cw.common.setStyle();
	$('#subImg').bind('click',subButton);
	validate();
	cw.common.getUserSession(getRoleInfo,true);
	loadFunctionTree();
})

//载入功能树
var functionTree;
function loadFunctionTree(){
	functionTree = new dTree('functionTree',cw.common.path + '/common/dtree/images');
	functionTree.add(1,-1,'功能列表');
	$.post(cw.common.path+'/json',
		{
			action : 'QUERY_FUNCTION_TREE_ACTION_BY_PK_ROLE',
			ROLE_ID : roleid
		}, function(data){
			if (data.success) {
				var size = data.root.T_SYS_FUNCTION.rs.length
				for(var i = 0; i < size; i++) {
					var obj=data.root.T_SYS_FUNCTION.rs[i];
					functionTree.add(obj.FUNCTION_ID, obj.PARENT_FUNCTION==''?1:obj.PARENT_FUNCTION,
						'FUNCTION_ID', obj.FUNCTION_ID, obj.FUNCTION_NAME, 
						obj.IS_ROLE_FUNCTION, null, null, null, null, null, null, obj.IS_ROLE_FUNCTION);
				}
				$('#funtree').html(functionTree.toString());
			} else {
				alert(data.errors.errmsg);
			}
		}, 'json');
}

function getRoleInfo(userinfo){
	var url = cw.common.path + '/json?action=GET_T_SYS_ROLE_ACTION&ROLE_ID='+roleid;
	$.post(url,{},function(data){
		if(data.success){
			var vobj=data.root.T_SYS_ROLE;
			if(vobj==null||vobj==''||vobj==undefined){
				alert("该角色不存在或已被删除");
				window.history.back();
				return;
			}
			else{
				var obj = data.root.T_SYS_ROLE.rs[0];
				$('#ROLE_ID').attr('value',roleid);
				$('#ROLE_NAME').attr('value',obj.ROLE_NAME);
				$('#ROLE_DESC').attr('value',obj.ROLE_DESC);
			}
		}
		else{
			alert(data.errors.errmsg);
		}
	},'json');
	
}

function subFrm(){
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		alert('已更新角色：'+$('#ROLE_NAME').val()+'的信息!');
	      		window.location.href="roleList.html";
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

function subButton(){
	return jQuery.formValidator.pageIsValid(1);
}

function validate(){
	$.formValidator.initConfig({formid:"frm",onerror:function(msg){alert(msg)},onsuccess:function(){subFrm();return false;}});
	$("#ROLE_NAME").formValidator({onshow:"请输入角色名称",oncorrect:"该角色名称可以使用",onfocus:"请输入角色名称"}).inputValidator({min:1,onerror:"角色名称必填"}).inputValidator({max:20,onerror:"真实姓名中文不超过10个字，英文不超过20个字符"})
	.functionValidator({
		fun:function (str,elem){
			var a = $.trim(str);
			if(a.length == 0) return false;
			return true;
		}, onerror: "角色名称必填"
	})
	.ajaxValidator({
		url : cw.common.path + "/json?action=CHECK_ROLE_BY_NAME_BY_UPDATE_ACTION",
		data : "ROLE_ID=" + roleid,
		isvalid : true,
		type : 'get',
		datatype : "json",
		success : function(data){
			if(data.success){
	            if(data.root.T_ROLE != undefined  && data.root.T_ROLE.rs[0] != null && data.root.T_ROLE.rs[0] != "" && data.root.T_ROLE.rs[0] != undefined){
	            	return false;
	            }
	            else{
	            	return true;
	            }
			}else{
				alert('出错了：'+data.errors.errmsg);
				return false;
			}
		},
		buttons: $("#subImg"),
		error: function(){alert("服务器没有返回数据，可能服务器忙，请重试");},
		onerror : "该角色名称已被占用，请更换角色名称",
		onwait : "正在对角色名称进行校验，请稍候..."
	});
	$("#ROLE_DESC").formValidator({empty:true,onshow:"请输入角色描述",oncorrect:"正确",onfocus:"请输入角色描述"}).inputValidator({max:128,onerror:"用户备注信息中文不超过64个字，英文不超过128个字符"});
	$("#funtree").formValidator({onshow:"请为角色分配模块权限",oncorrect:"正确",onfocus:"请为角色分配模块权限"})
	.functionValidator({
		fun:function (str,elem){
			if(getCheckedNum('FUNCTION_ID')==0){
				return false;
			}
			return true;
		}, onerror: "该角色未分配任何模块权限"
	})
}